package com.itheima.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

/**
 * @program: itheima-jdk8
 * @description:
 * @author: zhanghz001
 * @create: 2021-01-09 09:58
 **/
public class ThenAcceptDemo1 {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("执行子线程");
            System.out.println(Thread.currentThread().getName());
            
            return 123;
        }, pool);
        CompletableFuture<Void> future1 = future.thenAcceptAsync(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                System.out.println(integer);
                System.out.println(Thread.currentThread().getName());
            }
        }, pool);
        try {
            future1.get();
        } catch (InterruptedException e) {
            future1.cancel(true);
            e.printStackTrace();
        } catch (ExecutionException e) {
            future1.cancel(true);
            e.printStackTrace();
        } finally {
            pool.shutdown();
            
        }
    }
}
